package com.cui.project.service.impl.inner;

import com.api.common.model.entity.UserInterfaceInfo;
import com.api.common.service.InnerUserInterfaceInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.api.common.common.ErrorCode;
import com.cui.project.exception.BusinessException;
import com.cui.project.service.UserInterfaceInfoService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

/**
 * @author: 崔老爷
 * Date: 2024/9/28 20:53
 * @Description:
 */
@DubboService
public class InnerUserInterfaceInfoServiceImpl implements InnerUserInterfaceInfoService {

    @Resource
    private UserInterfaceInfoService userInterfaceInfoService;

    @Override
    public boolean invokeCount(long interfaceInfoId, long userId) {
        return userInterfaceInfoService.invokeCount(interfaceInfoId, userId) == 1;
    }

    @Override
    public int invokeInterfaceNum(Long userId, Long interfaceInfoId) {
        if (userId == null || interfaceInfoId == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        QueryWrapper<UserInterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("interfaceInfoId", interfaceInfoId);
        queryWrapper.select("id","leftNum");
        UserInterfaceInfo userInterfaceInfo = userInterfaceInfoService.getOne(queryWrapper);
        if (userInterfaceInfo == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }

        return userInterfaceInfo.getLeftNum();
    }
}
